import networkx as nx

# 读取数据集
G = nx.read_edgelist('email-Network.txt', create_using=nx.DiGraph())
# department_labels因为只有一个‘部门’属性用字典的方式存储
department_labels = {}
with open('email-Department.txt', 'r') as file:
    for line in file:
        # split()默认以空格的方式分割
        node, label = line.strip().split()
        department_labels[node] = label
# 将部门标签信息添加到节点属性中
nx.set_node_attributes(G, department_labels, 'department')

#雅卡尔指数
def jaccard_index(graph, node1, node2):
    neighbors1 = set(graph.neighbors(node1))
    neighbors2 = set(graph.neighbors(node2))
    #neighbors1.intersection(neighbors2)获取集合交集
    intersection = len(neighbors1.intersection(neighbors2))
    #neighbors1.union(neighbors2)获取并集
    union = len(neighbors1.union(neighbors2))
    jaccard = intersection / union
    return jaccard


for edge in G.edges:
    jaccard=jaccard_index(G,edge[0],edge[1])
    print(f"{edge[0]},{edge[1]},{jaccard}")